package com.ysw.environment;

import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.Properties;

/**
 * @ClassName EnvironmentFactoryBean
 * @Description TODO
 * @Author ysw
 * @Date 2021/10/29 0:05
 */
public class EnvironmentFactoryBean extends PropertiesFactoryBean {
    @Override
    protected Properties createProperties() throws IOException {
        Properties properties = super.createProperties();
        Properties environmentProperties = this.getEnvironmentProperties();
        if(environmentProperties != null) {
            CollectionUtils.mergePropertiesIntoMap(environmentProperties, properties);
        }
        return properties;
    }

    /**
     * 根据环境变量获取配置
     * @return
     * @throws IOException
     */
    protected Properties getEnvironmentProperties() throws IOException {
        return EnvironmentProperties.getAllProperties();
    }
}